package de.ipk_gatersleben.bit.bi.edal.publication;

import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.io.File;
import java.nio.file.Path;

/**
 * Utility class for some static functions for handling and converting
 * {@link String}s and so on.
 * 
 * @author arendd
 */
public class Utils {

	public static boolean checkIfStringIsEmpty(String string) {

		String temp = string.replaceAll(" ", "");
		temp = temp.replaceAll("\t", "");
		temp = temp.replaceAll("\n", "");

		return temp.isEmpty();
	}

	public static String cleanUpString(String string) {
		String temp = string.replaceAll(" ", "");
		temp = temp.replaceAll("\t", "");
		temp = temp.replaceAll("\n", "");
		return temp;
	}

	public static int numberOfFiles(Path srcDir) {

		File directory = srcDir.toFile();

		int count = 0;

		if (directory.isDirectory()) {
			File[] listFiles = directory.listFiles();
			for (int i = 0; i < listFiles.length; i++) {
				if (listFiles[i].isDirectory()) {
					count += numberOfFiles(listFiles[i].toPath());
				} else if (listFiles[i].isFile()) {
					count++;
				}
			}
			if(count>0){
			return count;
			}
		}
		return 1;
	}

	public static void addComponent(Container cont, GridBagLayout gbl,
			Component c, int x, int y, int width, int height, double weightx,
			double weighty, int ipadx, int ipady, Insets insets) {
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.ipadx = ipadx;
		gbc.ipady = ipady;
		gbc.gridx = x;
		gbc.gridy = y;
		gbc.gridwidth = width;
		gbc.gridheight = height;
		gbc.weightx = weightx;
		gbc.weighty = weighty;
		gbc.insets = insets;
		gbl.setConstraints(c, gbc);
		cont.add(c);
	}

	public static void add(Container container, GridBagLayout gridBagLayout,
			Component c, int gridx, int gridy) {
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.gridx = gridx;
		gbc.gridy = gridy;
		gbc.insets = new Insets(2, 5, 2, 5);
		gridBagLayout.setConstraints(c, gbc);
		container.add(c);
	}

	/**
	 * Add GridBagLayout Component for AttributeTablePanels
	 * 
	 * @param cont
	 * @param gbl
	 * @param c
	 * @param x
	 * @param y
	 * @param width
	 * @param height
	 * @param weightx
	 * @param weighty
	 * @param ipadx
	 * @param ipady
	 */
	public static void addComponent(Container cont, GridBagLayout gbl,
			Component c, int x, int y, int width, int height, double weightx,
			double weighty, int ipadx, int ipady) {
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.ipadx = ipadx;
		gbc.ipady = ipady;
		gbc.gridx = x;
		gbc.gridy = y;
		gbc.gridwidth = width;
		gbc.gridheight = height;
		gbc.weightx = weightx;
		gbc.weighty = weighty;
		gbc.insets = new Insets(0, 0, 0, 10);
		gbl.setConstraints(c, gbc);
		cont.add(c);
	}
}